package com.julang.check.config;

import com.julang.check.exception.ErrorTokenException;
import com.julang.check.exception.UserNotExistException;
import com.julang.check.exception.UserNotLoginException;
import com.julang.check.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static com.julang.check.utils.CommonData.*;
/**
 * @author 赵自强
 * */
@ControllerAdvice
public class ExceptionHandlerConfig {
    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    public Result handleUserNotExistException(UserNotExistException ex) {
        Result result = new Result();
        result.setMessage(ex.getMessage());
        result.setCode(USERNOTEXISTCODE);
        result.setData(ex.getToken());
        System.out.println(ex.toString());
        return result;
    }

    @ExceptionHandler(UserNotLoginException.class)
    @ResponseBody
    public Result handleUserNotLoginException(UserNotLoginException ex) {
        Result result = new Result();
        result.setMessage(ex.getMessage());
        result.setCode(USERNOTLOGINCODE);
        result.setData(ex.getToken());
        System.out.println(ex.toString());
        return result;
    }

    @ExceptionHandler(ErrorTokenException.class)
    @ResponseBody
    public Result handleErrorTokenException(ErrorTokenException ex) {
        Result result = new Result();
        result.setMessage(ex.getMessage());
        result.setCode(USERNOTEXISTCODE);
        result.setData(ex.getToken());
        System.out.println(ex.toString());
        return result;
    }
}
